//
//(C) Copyright 2011 HongKong Airline.,Ltd. 
//
/**
 * orderforminfoDaoHibernate
 *
 * 项目名称:代购系统
 *
 * @author Mengxiangliang
 *
 * 功能描述:
 * 
 * 历史版本:2013-01-04 v1.0.0 Mengxiangliang  创建:
 * 
 */
package com.gamutsoft.amb.orderinfo.dao.impl;

import java.util.List;
import java.util.Map;

import com.gamutsoft.amb.common.bean.QueryExample;
import com.gamutsoft.amb.common.constants.Constants;
import com.gamutsoft.amb.common.dao.impl.GenericDaoHibernate;
import com.gamutsoft.amb.common.exception.BusinessException;
import com.gamutsoft.amb.orderinfo.bean.CommitOrderBean;
import com.gamutsoft.amb.orderinfo.bean.OrderforminfoQueryBean;
import com.gamutsoft.amb.orderinfo.dao.IOrderforminfoDao;
import com.gamutsoft.amb.orderinfo.model.Orderforminfo;

@SuppressWarnings("unchecked")
public class OrderforminfoDaoHibernate extends GenericDaoHibernate<Orderforminfo, Long> implements IOrderforminfoDao {
    
     public OrderforminfoDaoHibernate() {
		super(Orderforminfo.class);

	}
    
    /**
	 * 根据条件查询记录
	 * @author Mengxiangliang
	 * @param  QueryExample
	 * @return List<Map>
	 * @throws BusinessException
	 */
	@Override
	public List<Map> searchOrderforminfoList(QueryExample qe) throws BusinessException {
	
	    StringBuffer sb = new StringBuffer();
		sb.append("SELECT T.ID,T.BUY_TYPE,T.CODE,T.SUMMONEY,T.STATUS,T.CREATETIME,T.USER_ID,T.BANK_NAME FROM ORDERFORMINFO T");
		//qe.addOrderBy("");
		return this.findByQueryExample(sb.toString(), qe);
		
	}

	/**
	 * 根据ID查询记录
	 * @author Mengxiangliang
	 * @param id
	 * @return
	 * @throws BusinessException
	 */
	@Override
	public Orderforminfo searchOrderforminfoById(Long id) throws BusinessException {
	
	  Orderforminfo domain = new Orderforminfo();
	   String hql = "from Orderforminfo where id=?";
	   List<Orderforminfo> list = this.getHibernateTemplate().find(hql, id);
	   if(list != null && list.size() >0){
		  domain = list.get(0);
	   }
	   return domain;
		
	}

	/**
	 * 删除记录
	 * @author Mengxiangliang
	 * @param id
	 * @throws BusinessException
	 */
	@Override
	public void deleteOrderforminfoById(Long id) throws BusinessException {
	
	 String hql ="delete from Orderforminfo where id=?";
	 this.getHibernateTemplate().bulkUpdate(hql, id);
	 
	}

	@Override
	public List<Map> searchOrderforminfoByWhere(QueryExample qe,
			OrderforminfoQueryBean bean) throws BusinessException {
		 StringBuffer sb = new StringBuffer();
			sb.append("SELECT T.ID,T.CODE,T.BUY_TYPE,T.SUMMONEY,T.STATUS,T.CREATETIME,T.USER_ID,T.BANK_NAME FROM ORDERFORMINFO T");
			
			if(bean!=null){
				if(bean.getUserId()!=null){
					qe.addCondictions("T.USER_ID","=",bean.getUserId());
				}
				if(bean.getStatus()!=null){
					qe.addCondictions("T.STATUS","=",bean.getStatus());
				}
			}
			qe.addOrderBy("T.CREATETIME ");
			return this.findByQueryExample(sb.toString(), qe);
	}

	@Override
	public List<Map> searchOrderforminfoByStatus(QueryExample qe, String status,Long userId)
			throws BusinessException {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT T.ID,O.BUY_TYPE,T.URL,T.DOMAINNAME,T.NAME,T.SHOPNAME,T.PRICE,T.BUYCOUNT,T.STATUS,T.FREIGHT,T.REMARK,T.STATUSNODE,T.IMGURL,T.ORDERFORMINFOID,T.PACKAGESTATUS,T.PACKAGEID,T.CREATE_TIME FROM COMMODITY T");
		sb.append(" LEFT JOIN ORDERFORMINFO O ON T.ORDERFORMINFOID=O.ID ");
		qe.addCondictions("O.STATUS","=",Constants.ORDERINFO_SUCCESS);//支付成功
		if(status!=null && !"".equals(status)){
		   qe.addCondictions("T.STATUS","=",status);
		}
		if(userId!=null){
		  qe.addCondictions("O.USER_ID","=",userId);
		}
		qe.setMySql("  T.PACKAGEID IS NULL ");
		return this.findByQueryExample(sb.toString(), qe);
	}

	@Override
	public List<Map> searchOrderforminfoByWhere(QueryExample qe,
			CommitOrderBean bean) throws BusinessException {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT T.ID,T.URL,T.DOMAINNAME,O.BUY_TYPE,T.NAME,T.SHOPNAME,T.PRICE,T.BUYCOUNT,T.FREIGHT,T.REMARK,T.STATUS,T.STATUSNODE,T.IMGURL,T.ORDERFORMINFOID,T.PACKAGESTATUS,T.PACKAGEID,T.CREATE_TIME FROM COMMODITY T");
		sb.append(" LEFT JOIN ORDERFORMINFO O ON T.ORDERFORMINFOID=O.ID ");
		qe.addCondictions("O.STATUS","=",Constants.ORDERINFO_SUCCESS);
		
		if(bean!=null){
			if(bean.getName()!=null && !"".equals(bean.getName())){
				qe.addCondictions("T.NAME","like","%"+bean.getName()+"%");
			}
			if(bean.getStatus()!=null && !"".equals(bean.getStatus())){
				qe.addCondictions("T.STATUS","=",bean.getStatus());
			}
			if(bean.getSearchMonth()!=null && !"".equals(bean.getSearchMonth())){
				qe.addCondictions("O.CREATE_TIME","like",bean.getSearchMonth()+"%");
			}
			if(bean.getUserId()!=null){
				qe.addCondictions("O.USER_ID","=",bean.getUserId());
			}
			if(bean.getBuyType()!=null && !"".equals(bean.getBuyType())){
				qe.addCondictions("O.BUY_TYPE","=",bean.getBuyType()+"%");
			}
		}
		qe.setMySql("  T.PACKAGEID IS NULL ");
		return this.findByQueryExample(sb.toString(), qe);
	}
}